package com.neuedu.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 项目：springmvc-java4
 * 创建时间：  2022-03-22   15:29
 * 作者 :jshand
 * 描述 :
 */
@Controller //作为bean对象存在
public class UserController {

    /**
     * http://127.0.0.1:8080/springmvc/user_test
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/user_test" )
    public void test(HttpServletRequest request , HttpServletResponse response) throws IOException {
        System.out.println("使用注解形式开发的代码");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("<h3>使用注解形式开发的代码</h3>");
        out.flush();
        out.close();
    }


    /**
     *  http://127.0.0.1:8080/springmvc/post
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/post",method = {RequestMethod.POST,RequestMethod.GET})
    public void postMethod(HttpServletRequest request , HttpServletResponse response) throws IOException {
        System.out.println("Post请求");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("<h3>使用注解形式开发的代码</h3>");
        out.flush();
        out.close();
    }

    /**
     * 针对header过滤
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/head",headers = "User-Agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36")
    public void head(HttpServletRequest request , HttpServletResponse response) throws IOException {
        System.out.println("Header");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("<h3>Header过滤</h3>");
        out.flush();
        out.close();
    }




    @RequestMapping(value = "/save",params ={"!username"} )
    public void save(HttpServletRequest request , HttpServletResponse response) throws IOException {
        System.out.println("Header");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("<h3>Header过滤</h3>");
        out.flush();
        out.close();
    }



//    @RequestMapping(value = "/do_get",method = RequestMethod.GET)
    @GetMapping("/do_get")
    public void doGet(){

    }


//    @RequestMapping(value = "/do_post",method = RequestMethod.Post)
    @PostMapping("/do_post")
    public void doPost(){

    }









}
